var pageSize = 10;
var userTypeMap = {};
$(document).ready(function(){
	commonSet();
	//加载查询条件
	initQueryCondition();
	//表格初始化
	initTable();
	//事件绑定
	eventBind();
});

function commonSet(){
	initLoader();
	//用户类型
	userTypeMap = getTransParam('userTypeInfo');
}

//事件绑定
function eventBind(){
	//带有分页信息的查询
	$("#queryBtn").click(pageQuery).click();
	
	//添加按钮弹出层
	$("#addBtn").click(showAddFormDialog);
}

//加载查询条件
function initQueryCondition(){
	$.getApiClient('/baseInfo/hospitals').get().done(function(data){
		if(!data.payload){
			data.payload=[];
		}
	    multipleSelectData("hospital",[{"id": "", "hospital_name": "请选择"}].concat(data.payload), null, {valueAttr:'id', displayAttr:'hospital_name'},"请选择");
		$('#hospital').multipleSelect({width:'100%', single: true});
	});
	
	
	$('#hospital').change(function(){
		var hosid=$(this).children('option:selected').val();
		loadDetpSelect(hosid);
	}).change();
	
	function loadDetpSelect(hosId){
		if(hosId){
			$.getApiClient('/baseInfo/departments').setQuery({hospital_id:hosId}).get().done(function(data){
				if(!data.payload){
					data.payload=[];
				}
			    multipleSelectData("department",[{"id": "", "department_name": "请选择"}].concat(data.payload), null, {valueAttr:'id', displayAttr:'department_name'},"请选择");
				$('#department').multipleSelect({width:'100%', single: true});
			});
		}else{
			multipleSelectData("department",[{"id": "", "department_name": "请选择"}], null, {valueAttr:'id', displayAttr:'department_name'},"请选择");
			$('#department').multipleSelect({width:'100%', single: true});
		}
	}
}

//表格初始化
function initTable(){
	$('#userListTable').bootstrapTable({
		onPageChange: function (number, size) {
			pageSize = size;
			query(number, size);
		}
	});
}

//带有分页信息的查询
function pageQuery(){
	query(1, pageSize);
};

//分页查询
function query(pageNumber, pageSize){
	var queryCondition={
		pageNumber: pageNumber, 
		pageSize: pageSize,
		params:$("#queryForm").collect()
	}
	
	$.getApiClient('/user/usersPager').setQuery(queryCondition).get().done(function(data){
	    $('#userListTable').bootstrapTable('load', data.payload);
	});
};

//添加用户弹出
function showAddFormDialog(){
	customRemoteUrlDialog('添加用户','/page/business/sysmanage/user/form?type=add',null);
}

//修改用户弹出
function showEditFormDialog(row){
	customRemoteUrlDialog('修改用户','/page/business/sysmanage/user/form?type=edit&userId='+row.id,null);
}

//删除用户弹出
function showDelFormDialog(row){
	customConfirmDialog('是否确认删除用户？','删除用户',null,function(dialog){
		//执行删除
		dialog.close();
		doDeleteUser(row.id,function(data){
			if(data.code == "0"){
				customTimeOutDialog('信息提示','删除用户成功',null,2000);
				pageQuery();
			}else{
				customAlertDialog(data.message, '错误提示', null, null);
			}
		})
	});
	
	function doDeleteUser(userId,callBack){
		$.getApiClient('/user/{userid}').setQuery({
			userid : userId
		}).del().done(function(data){
			callBack(data)
		});
	}
}

//操作列
function operateFormatter(value, row, index) {
	var oprArray=[];
	oprArray.push('<a class="edit" href="javascript:void(0)" title="修改"><i class="fa fa-edit"></i>修改</a>');
	oprArray.push('&nbsp;&nbsp;&nbsp;')
	//登录用户不可删除自己
	if(row.id!=loginUser.id){
		oprArray.push('<a class="delete" href="javascript:void(0)" title="删除"><i class="fa fa-close"></i>删除</a>')
	}
	return oprArray.join('');
};

operateEvents = {
	'click .edit' : function(e, value, row, index) {
		showEditFormDialog(row);
	},
	'click .delete' : function(e, value, row, index) {
		showDelFormDialog(row);
	}
};